package org.hbbTlias2025.controller;


import lombok.extern.slf4j.Slf4j;
import org.hbbTlias2025.anno.LogOpreation;
import org.hbbTlias2025.pojo.Emp;
import org.hbbTlias2025.pojo.EmpQueryParam;
import org.hbbTlias2025.pojo.Result;
import org.hbbTlias2025.pojo.pageReturn;
import org.hbbTlias2025.service.EmpService;
//import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RequestMapping("/emps")
@RestController
public class EmpController {



    @Autowired
    private EmpService empService;

//    查询所有员工信息
//    @GetMapping
//    public Result findAll(@RequestParam(defaultValue = "1") Integer page,
//                          @RequestParam(defaultValue = "10") Integer pageSize,
//                          String name, String gender,
//                          @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
//                          @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end) {
//        log.info("查询所有员工信息");
//        log.info("page:{},pageSize:{},name:{},gender:{},begin:{},end:{}", page, pageSize, name, gender, begin, end);
//
//        try {
//            pageReturn<Emp> emps = empService.findAll(page, pageSize, name, gender, begin, end);
//            return Result.success(emps);
//        }catch (Exception e){
//            return Result.error("查询失败了5555");
//        }
//    }

    @GetMapping
    public Result findAll(EmpQueryParam emp) {
        log.info("查询所有员工信息: {}" , emp);
//        log.info("page:{},pageSize:{},name:{},gender:{},begin:{},end:{}", emp);

        try {
            pageReturn<Emp> emps = empService.findAll(emp);
            return Result.success(emps);
        }catch (Exception e){
            return Result.error("查询失败了5555");
        }
    }

    @PostMapping
//    @LogOpreation
    public Result addOne(@RequestBody Emp emp) {
        log.info("添加员工信息: {}" , emp);
        try {
            int i = empService.addOne(emp);
            if (i >= 1) {
                return Result.success();
            }
        } catch (Exception e){
            return Result.error("添加失败" + e);
        }
        return Result.success();
    }


    @DeleteMapping
    @LogOpreation
    public Result deleteOne(@RequestParam List<Integer> ids) {
        log.info("现在删除员工信息——————{}", ids);
        empService.deleteIds(ids);
        return Result.success();
    }


    @GetMapping("/{id}")
    public Result getOne(@PathVariable Integer id) {
        log.info("现在获取员工信息——————{}", id);
        Emp emp = null;
        try {
            emp = empService.getOne(id);
            return Result.success(emp);

        } catch (Exception e) {
            log.error("获取员工信息失败: {}", String.valueOf(e));
            return Result.error(String.valueOf(e));
        }
    }

    @PutMapping
    @LogOpreation
    public Result updateOne(@RequestBody Emp emp) {
        log.info("修改员工信息: {}" , emp);
//        try {
//            int i = empService.updateOne(emp);
//            if (i >= 1) {
//                return Result.success();
//            }
//        } catch (Exception e){
//            log.error("修改失败: {}", String.valueOf(e));
//            return Result.error("修改失败" + e);
//        }
//        return Result.success();

        int i = empService.updateOne(emp);
        if (i >= 1) {
            return Result.success();
        }
        return Result.success();
    }


    @GetMapping("/list")
//    @LogOpreation
    public Result getAllEmp() {
        try {
            List<Emp> emps = empService.getAllEmpName();
            return Result.success(emps);
        } catch (Exception e) {
            throw new RuntimeException("查询失败我抄" + e.getMessage());
        }
    }
}
